import axios from 'axios'
import { useUserStore } from '@/stores/stores.js'

const request = axios.create({
  baseURL: import.meta.env.VITE_API_URL + '/api'
})

request.interceptors.request.use((config) => {
  const userStore = useUserStore()
  if (userStore.user.token) {
    config.headers.Authorization = `Bearer ${userStore.user.token}`
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use((response) => {
  return response.data
}, (error) => {
  return Promise.reject(error)
})

export default request